package org.eden.karb.persistence.dao.impl;

import org.eden.karb.persistence.dao.AbstractDAO;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.io.Serializable;

/**
 * @author trsimoes
 */
@Repository
@Component
public abstract class AbstractDAOImpl<T> implements AbstractDAO<T> {

    @PersistenceContext
    protected EntityManager entityManager;

    public <T, PK extends Serializable> T findById(Class<T> entity, PK id) {
        return entityManager.find(entity, id);
    }

    public void persist(T entity) {
        entityManager.persist(entity);
    }

    public T merge(T entity) {
        return entityManager.merge(entity);
    }

    public void delete(T entity) {
        entityManager.remove(entity);
    }
}
